package at.tugraz.akm.forume;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import at.tugraz.akm.forume.adapter.ThreadAdapter;
import at.tugraz.akm.forume.bean.Thread;
import at.tugraz.akm.forume.extras.MySharedPreferences;
import at.tugraz.akm.forume.view.PullToRefreshListView;
import at.tugraz.akm.forume.view.PullToRefreshListView.OnRefreshListener;
import at.tugraz.akm.forume.webservice.Client;

public class Main extends ListActivity {

	ArrayList<Thread> threads;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// MySharedPreferences.print(this);

		threads = Client.getThreadsSusbcribed(getApplicationContext());

		ImageView new_comment = (ImageView) findViewById(R.id.new_thread);
		ImageView new_comment2 = (ImageView) findViewById(R.id.create2);
		new_comment.setOnClickListener(ocl_new_comment);
		new_comment2.setOnClickListener(ocl_new_comment);

		ImageView sync = (ImageView) findViewById(R.id.sync);
		ImageView sync2 = (ImageView) findViewById(R.id.sync2);
		sync.setOnClickListener(ocl_sync);
		sync2.setOnClickListener(ocl_sync);

		ImageView search = (ImageView) findViewById(R.id.search);
		ImageView search2 = (ImageView) findViewById(R.id.search2);
		search.setOnClickListener(ocl_search);
		search2.setOnClickListener(ocl_search);

		ImageView new_subs = (ImageView) findViewById(R.id.new_subs);
		ImageView new_subs2 = (ImageView) findViewById(R.id.new_subs2);
		new_subs.setOnClickListener(ocl_new_sub);
		new_subs2.setOnClickListener(ocl_new_sub);

		((PullToRefreshListView) getListView())
				.setOnRefreshListener(new OnRefreshListener() {
					@Override
					public void onRefresh() {
						new GetNewThreadsTaks().execute();
					}
				});

		ThreadAdapter ta = new ThreadAdapter(this, R.layout.thread_row, threads);
		setListAdapter(ta);
	}

	OnClickListener ocl_new_comment = new OnClickListener() {

		@Override
		public void onClick(View v) {
			v.getContext()
					.startActivity(new Intent(Main.this, NewThread.class));
		}
	};

	OnClickListener ocl_search = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onSearchRequested();
		}
	};

	OnClickListener ocl_sync = new OnClickListener() {

		@Override
		public void onClick(View v) {
			threads = Client.getThreadsSusbcribed(getApplicationContext());
			ThreadAdapter ta = new ThreadAdapter(getApplicationContext(),
					R.layout.thread_row, threads);
			setListAdapter(ta);
			((PullToRefreshListView) getListView()).onRefreshComplete();
		}
	};

	OnClickListener ocl_new_sub = new OnClickListener() {

		@Override
		public void onClick(View v) {
			final Dialog dialog = new Dialog(Main.this);
			dialog.setContentView(R.layout.dialog_subscribe);
			dialog.setTitle(R.string.subscribe_thread);
			final EditText edit = (EditText) dialog.findViewById(R.id.url_subs);
			Button subs = (Button) dialog.findViewById(R.id.btn_subs);
			subs.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {

					String text = edit.getText().toString();
					if (text != null && !text.isEmpty()) {
						if (Client.subs(getApplicationContext(), text)) {
							threads = Client
									.getThreadsSusbcribed(getApplicationContext());
							ThreadAdapter ta = new ThreadAdapter(
									getApplicationContext(),
									R.layout.thread_row, threads);
							setListAdapter(ta);
							((PullToRefreshListView) getListView())
									.onRefreshComplete();
							dialog.dismiss();
						} else {
							dialog.findViewById(R.id.error_subs).setVisibility(
									View.VISIBLE);
						}
					}
				}
			});

			Button cancel = (Button) dialog.findViewById(R.id.btn_cancel);
			cancel.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					dialog.dismiss();
				}
			});

			dialog.show();
		}
	};

	public class GetNewThreadsTaks extends AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			try {
				java.lang.Thread.sleep(2000);
			} catch (InterruptedException e) {
				;
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void voids) {
			Toast.makeText(getApplicationContext(), "Updated",
					Toast.LENGTH_SHORT).show();

			threads = Client.getThreadsSusbcribed(getApplicationContext());
			ThreadAdapter ta = new ThreadAdapter(getApplicationContext(),
					R.layout.thread_row, threads);
			setListAdapter(ta);
			((PullToRefreshListView) getListView()).onRefreshComplete();
			super.onPostExecute(voids);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.logout:
			MySharedPreferences.clear(getApplicationContext());
			startActivity(new Intent(Main.this, Login.class));
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}